package com.hdlang.assists.ad.ui.activity

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import androidx.appcompat.app.AlertDialog
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.hdlang.assists.ad.utils.AccessibilityServiceTool

class SplashActivity : BaseActivity() {

    private var dialog: AlertDialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onResume() {
        super.onResume()
        accessibilityDialog()
    }


    private fun getRequestPermissions(permissions: Array<out String>): Array<String> {
        val list = ArrayList<String>()
        for (permission in permissions) {
            if (checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED) {
                list.add(permission)
            }
        }
        return list.toTypedArray()
    }

    private fun accessibilityDialog() {
        val requestPermissions: Array<String> = getRequestPermissions(
            arrayOf(
                Manifest.permission.READ_PHONE_STATE
            )
        )
        if (requestPermissions.isEmpty()) {
            if (AccessibilityServiceTool.isAccessibilityServiceEnabled(
                    this
                )
            ) {
                goMain()
            } else {
                if (dialog == null) {
                    dialog = MaterialAlertDialogBuilder(this).setCancelable(false)
                        .setTitle("提示").setMessage("申请无障碍功能")
                        .setNegativeButton(
                            "去设置"
                        ) { dialog, _ ->
                            dialog?.dismiss()
                            enableAccessibility()
                        }
                        .setPositiveButton(
                            "申请"
                        ) { dialog, _ ->
                            dialog?.dismiss()
                            enableAccessibility()
                        }.create()
                }
                dialog?.setCanceledOnTouchOutside(false)
                if (dialog?.isShowing != true) {
                    dialog?.show()
                }
            }
        } else {
            goSettings()
        }
    }

    private fun enableAccessibility() {
        if (AccessibilityServiceTool.isAccessibilityServiceEnabled(this)) {
            goMain()
        } else {
            AccessibilityServiceTool.enableAccessibilityService(this)
        }
    }

    private fun goMain() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    private fun goSettings() {
        val intent = Intent()
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.setData(Uri.fromParts("package", packageName, null))
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(intent)
    }
}